package com.letv.core.bean;

import java.io.File;
import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import android.text.TextUtils;

import com.letv.core.constant.DatabaseConstant;
import com.letv.core.constant.DownloadConstant;
import com.letv.core.utils.BaseTypeUtils;
import com.letv.core.utils.DownloadUtils;

/**
 * DownloadDBListBean
 * 
 * @author 吴新荣
 * DownloadDBBean作为静态内部类
 * @see DownloadDBBean
 */
public class DownloadDBListBean extends ArrayList<DownloadDBListBean.DownloadDBBean> implements LetvBaseBean {

    private static final long serialVersionUID = 1L;

    public static class DownloadDBBean implements LetvBaseBean {
        private static final long serialVersionUID = 1L;

        private int vid;

        private String mmsid;

        private int aid;

        private String icon;

        /**
         * 页面类型：1-剧集形式,2-列表形式,3-单片形式；非以上类型，默认为进列表形式
         */
        private int type;

        private float ord;

        private int cid;

        private String episodetitle;

        /**
         * 5.1.2 新增字段
         */
        private String episodeIcon;

        private String albumtitle;

        private long totalsize;// 文件总大小
        /**
         * TOSTART(0),加入下载队列 STARTED(1),正在下载中 STOPPED(3),停止 FINISHED(4),下载完成
         * ERROR(5) 下载错误
         */
        private int finish = 0;

        private long timestamp;

        private long length;// 已下载文件大小

        private String filePath = DownloadConstant.DOWNLOAD_LOCATION_DIR;

        private int isHd;

        /**
         * 0 旧数据， 1 新数据
         */
        private int isNew = 1;

        private File file;

        /**
         * 片头时间
         */
        private long btime;
        /**
         * 片尾时间
         */
        private long etime;

        /**
         * 下载完成之后，是否观看,默认为0，未观看
         */
        private int isWatch = 0;

        /**
         * 时长
         */
        private long duration;
        
        private String videoTypeKey = "";

        public DownloadDBBean() {

        }

        public DownloadDBBean(DownloadDBBean bean) {
            super();
            this.vid = bean.vid;
            this.mmsid = bean.mmsid;
            this.aid = bean.aid;
            this.icon = bean.icon;
            this.type = bean.type;
            this.ord = bean.ord;
            this.cid = bean.cid;
            this.episodetitle = bean.episodetitle;
            this.albumtitle = bean.albumtitle;
            this.totalsize = bean.totalsize;
            this.finish = bean.finish;
            this.timestamp = bean.timestamp;
            this.length = bean.length;
            this.filePath = bean.filePath;
            this.isHd = bean.isHd;
            this.isNew = bean.isNew;
            this.file = bean.file;
            this.btime = bean.btime;
            this.etime = bean.etime;
            this.isWatch = bean.isWatch;
        }

        public static DownloadDBBean getInstance(AlbumNew album, VideoBean video, int isHd) {
            DownloadDBBean bean = new DownloadDBBean();
            if (album != null) { // 专辑
                bean.setAid((int) album.getId());
                bean.setAlbumtitle(album.getNameCn());
                bean.setCid(album.getCid());
                bean.setType(Integer.parseInt(album.getStyle()));
                bean.setIcon(album.getPic());
                bean.setEpisodeIcon(!TextUtils.isEmpty(video.getPic()) ? video.getPic() : album.getPic());
            } else { // 单视频
                // mDownloadDBBean.setAlbumId(0);
                bean.setAid((int) video.getId());
                bean.setAlbumtitle("");
                bean.setCid(video.getCid());
                bean.setType(0);
                bean.setIcon(video.getPic());
                bean.setEpisodeIcon(video.getPic());
            }
            bean.setDuration(video.getDuration());
            bean.setIsNew(1);
            bean.setVid((int) video.getId());
            bean.setEpisodetitle(video.getNameCn());
            bean.setFinish(0);
            bean.setFilePath(DownloadUtils.getDownloadDir().getAbsolutePath());
            bean.setMmsid(video.getMid());
            bean.setOrd(BaseTypeUtils.stof(video.getEpisode()));
            bean.setIsHd(isHd);
            bean.setBtime(video.getBtime());
            bean.setEtime(video.getEtime());
            bean.setIsWatch(0); // 默认观看为0

            return bean;
        }

        public DownloadDBBean(JSONObject jsonObject) throws JSONException {
            if (jsonObject != null) {
                this.vid = jsonObject.getInt("episodeid");
                this.aid = jsonObject.getInt("albumId");
                this.icon = jsonObject.getString("icon");
                this.type = jsonObject.getInt("type");
                this.ord = jsonObject.getInt("ord");
                this.cid = jsonObject.getInt("cid");
                this.episodetitle = jsonObject.getString("episodetitle");
                this.albumtitle = jsonObject.getString("albumtitle");
                this.totalsize = jsonObject.getInt("totalsize");
                this.finish = jsonObject.getInt("finish");
                this.timestamp = jsonObject.getInt("timestamp");
                this.length = jsonObject.getInt("length");
                if (jsonObject.has("filePath")) {
                    this.filePath = jsonObject.getString("filePath");
                }
                this.isHd = jsonObject.getInt("isHd");
                this.isNew = jsonObject.getInt("isNew");
                this.btime = jsonObject.getLong("btime");
                this.etime = jsonObject.getLong("etime");
                // this.isWatch = 0;
            }
        }

        public long getDuration() {
            return duration;
        }

        public void setDuration(long duration) {
            this.duration = duration;
        }

        public int getIsWatch() {
            return isWatch;
        }

        public void setIsWatch(int isWatch) {
            this.isWatch = isWatch;
        }

        public int getVid() {
            return vid;
        }

        public void setVid(int vid) {
            this.vid = vid;
        }

        public String getMmsid() {
            return mmsid;
        }

        public void setMmsid(String mmsid) {
            this.mmsid = mmsid;
        }

        public int getAid() {
            return aid;
        }

        public void setAid(int aid) {
            this.aid = aid;
        }

        public String getIcon() {
            return icon;
        }

        public void setIcon(String icon) {
            this.icon = icon;
        }

        public int getType() {
            return type;
        }

        public void setType(int type) {
            this.type = type;
        }

        public float getOrd() {
            return ord;
        }

        public void setOrd(float ord) {
            this.ord = ord;
        }

        public String getEpisodetitle() {
            return episodetitle;
        }

        public void setEpisodetitle(String episodetitle) {
            this.episodetitle = episodetitle;
        }

        public String getEpisodeIcon() {
            return episodeIcon;
        }

        public void setEpisodeIcon(String episodeIcon) {
            this.episodeIcon = episodeIcon;
        }

        public String getAlbumtitle() {
            return albumtitle;
        }

        public void setAlbumtitle(String albumtitle) {
            this.albumtitle = albumtitle;
        }

        public long getTotalsize() {
            return totalsize;
        }

        public void setTotalsize(long totalsize) {
            this.totalsize = totalsize;
        }

        public int getFinish() {
            return finish;
        }

        public void setFinish(int finish) {
            this.finish = finish;
        }

        public long getTimestamp() {
            return timestamp;
        }

        public void setTimestamp(long timestamp) {
            this.timestamp = timestamp;
        }

        public long getLength() {
            return length;
        }

        public void setLength(long length) {
            this.length = length;
        }

        public int getCid() {
            return cid;
        }

        public void setCid(int cid) {
            this.cid = cid;
        }

        public String getFilePath() {
            return filePath;
        }

        public void setFilePath(String filePath) {
            this.filePath = filePath;
        }

        public int getIsHd() {
            return isHd;
        }

        public void setIsHd(int isHd) {
            this.isHd = isHd;
        }

        public int getIsNew() {
            return isNew;
        }

        public void setIsNew(int isNew) {
            this.isNew = isNew;
        }

        public File getFile() {
            return file;
        }

        public void setFile(File file) {
            this.file = file;
        }

        public long getBtime() {
            return btime;
        }

        public void setBtime(long btime) {
            this.btime = btime;
        }

        public long getEtime() {
            return etime;
        }

        public void setEtime(long etime) {
            this.etime = etime;
        }

        public String getVideoTypeKey() {
            return videoTypeKey;
        }

        public void setVideoTypeKey(String videoTypeKey) {
            this.videoTypeKey = videoTypeKey;
        }

        public JSONObject toJson() throws JSONException {

            JSONObject jsonObject = new JSONObject();

            jsonObject.put(DatabaseConstant.DownloadTrace.Field.EPISODE_ID, vid);
            jsonObject.put(DatabaseConstant.DownloadTrace.Field.ALBUM_ID, aid);
            jsonObject.put(DatabaseConstant.DownloadTrace.Field.ICON, icon);
            jsonObject.put(DatabaseConstant.DownloadTrace.Field.TYPE, type);
            jsonObject.put(DatabaseConstant.DownloadTrace.Field.ORDER, ord);
            jsonObject.put(DatabaseConstant.DownloadTrace.Field.CID, cid);
            jsonObject.put(DatabaseConstant.DownloadTrace.Field.EPISODE_TITLE, episodetitle);
            jsonObject.put(DatabaseConstant.DownloadTrace.Field.EPISODE_ICON, episodeIcon);
            jsonObject.put(DatabaseConstant.DownloadTrace.Field.ALBUM_TITLE, albumtitle);
            jsonObject.put(DatabaseConstant.DownloadTrace.Field.TOTAL_SIZE, totalsize);
            jsonObject.put(DatabaseConstant.DownloadTrace.Field.FINISH, finish);
            jsonObject.put(DatabaseConstant.DownloadTrace.Field.TIMESTAMP, timestamp);
            jsonObject.put(DatabaseConstant.DownloadTrace.Field.LENGTH, length);

            /**
             * 4.1及以前版本filePath对应字段为 jsonObject.put("filePath", filePath);
             * 因此在5.1版本对解析该字段进行兼容
             */
            jsonObject.put(DatabaseConstant.DownloadTrace.Field.FILE_PATH, filePath);
            jsonObject.put(DatabaseConstant.DownloadTrace.Field.ISHD, isHd);
            jsonObject.put(DatabaseConstant.DownloadTrace.Field.IS_NEW, isNew);
            jsonObject.put(DatabaseConstant.DownloadTrace.Field.B_TIME, btime);
            jsonObject.put(DatabaseConstant.DownloadTrace.Field.E_TIME, etime);
            jsonObject.put(DatabaseConstant.DownloadTrace.Field.IS_WATCH, isWatch);
            jsonObject.put(DatabaseConstant.DownloadTrace.Field.DURATION, duration);
            return jsonObject;
        }

        @Override
        public String toString() {
            return "DownloadDBBean [episodeid=" + vid + ", mmsid=" + mmsid + ", albumId=" + aid + ", icon="
                    + icon + ", type=" + type + ", ord=" + ord + ", cid=" + cid + ", episodetitle=" + episodetitle
                    + ", albumtitle=" + albumtitle + ", totalsize=" + totalsize + ", finish=" + finish + ", timestamp="
                    + timestamp + ", length=" + length + ", filePath=" + filePath + ", isHd=" + isHd + ",isNew ="
                    + isNew + ",btime=" + btime + ",etime=" + etime + ",isWatch=" + isWatch + "]";
        }

    }
}
